<?php

namespace app\admin\controller\qingdongwms\order;

use addons\qingdongwms\model\Contacts;
use addons\qingdongwms\model\Contract;
use addons\qingdongwms\model\StaffSignIn;
use app\admin\controller\qingdongwms\Base;
use addons\qingdongwms\model\Form;
use addons\qingdongwms\model\Staff;
use addons\qingdongwms\model\Customer;
use addons\qingdongwms\model\Order as OrderModel;
use think\DB;

/**
 * 订单管理
 */
class Order extends Base {
    public function _initialize() {
        parent::_initialize();
        $this->model = new OrderModel();
    }

    /**
     * 订单列表
     */
    public function index() {
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list   = $this->model->with([ 'customer','contract','contacts','ownerStaff','createStaff'])->where($where)->where('create_staff_id', 'in', Staff::getMyStaffIds())->order($sort, $order)->paginate($limit);
            $rows    = $list->items();
            foreach ($rows as &$v){
                $v['owner_staff_id']=$v['owner_staff']['name']??'';
            }
            $result = array("total" => $list->total(), "rows" => $rows);

            return json($result);
        }

        return $this->view->fetch();
    }


    /**
     * 订单详情
     */
    public function detail($ids = null) {
        $order=OrderModel::where(['id'=>$ids])->with(['customer','contract','contacts','owner_staff','createStaff'])->find();
        $order['record']=StaffSignIn::where(['relation_type'=>StaffSignIn::ORDER_TYPE,'relation_id'=>$ids])
            ->field('id,staff_id,relation_type,relation_id,content,createtime,staff_id')->select();
        $order = $order->toArray();
        $this->assign('order',$order);
        $this->assign('ids',$ids);
        $this->assign('form_data', Form::getDataValue('订单管理'));
        $this->assign('row',$order);
        return $this->view->fetch();
    }
// 获取产品
    public function get_product($ids = null) {
        $contract_id = input('get.contract_id');
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $list = \addons\qingdongwms\model\ContractProduct::where(['contract_id'=>$contract_id])->with(['product'])->order('id desc')->paginate($limit);

        $result = array("total" => $list->total(), "rows" => $list->items());

        return json($result);
    }
// 获取产品
    public function get_record($ids = null) {
        $order_id = input('get.order_id');
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $list = StaffSignIn::where(['relation_type'=>StaffSignIn::ORDER_TYPE,'relation_id'=>$order_id])
            ->field('id,staff_id,relation_type,relation_id,content,file_ids,createtime,staff_id')->order('id','desc')->paginate($limit);

        $result = array("total" => $list->total(), "rows" => $list->items());

        return json($result);
    }
    //录入到账信息
    public function input_payment($ids = null){
        if ($this->request->isAjax()) {
            $data   = $this->request->post('row/a');
            $id =   $ids;
            $payment_date   =   $data['payment_date'];
            $file_ids       =   $data['file_ids'];
            $remarks        =   $data['remarks'];

            if(empty($payment_date)){
                $this->error('参数不能为空');
            }
            //已到账
            $this->model->save(['is_account'=>1],['id'=>$ids]);

            $content='到账时间:'.$payment_date.'<br>备注:'.$remarks;
            $data = [
                'staff_id' => $this->_staff->id,
                'relation_type' => StaffSignIn::ORDER_TYPE,//日程类型
                'relation_id' => $id,//签到关联id
                'content' => $content,//处理结果
                'file_ids' => $file_ids,
            ];
            StaffSignIn::createSignIn($data);

            $this->success('录入成功');
        }
        return $this->view->fetch();
    }

}